package pl.un4giv3n.mightvsmagic.action.clan;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.Query;

import pl.un4giv3n.mightvsmagic.entity.User;

public class ClanConfigPageAction extends ClanAction{

	private static final long serialVersionUID = 1L;
	private List<User> pendingUsers;
	private List<User> clanUsers;

	@Override
	public String execute() {
		EntityManager entityManager = getEntityManager();
		user = getSessionUser(entityManager);
		if (user == null) {
			addActionError("Zaloguj się");
			entityManager.close();
			return LOGIN;
		}
		clan = user.getClan();
		pendingUsers = findPendingUsers(entityManager);
		clanUsers = clan.getUsers();
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	private List<User> findPendingUsers(EntityManager em) {
		Query query = em.createNamedQuery("find.user.pending.clan");
		query.setParameter("clanId", clan.getId());
		return query.getResultList();
	}

	public List<User> getPendingUsers() {
		return pendingUsers;
	}

	public List<User> getClanUsers() {
		return clanUsers;
	}
	
}
